package cn.semes.controller;

import cn.semes.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getUserFallback")
    public User print(@PathVariable("id") Long id){
        String url="http://user-server/user/"+id;

        // 此处就是Ribbon调用，此处如果调用不通那么就熔断
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    public User getUserFallback(@PathVariable("id") Long id){
        // 响应托底数据
        return new User(0L, "熔断！", "服务不可调用！");
    }


}
